package top.drinyeit.weilong.presenter;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import top.drinyeit.weilong.activity.CommodityDetailActivity;
import top.drinyeit.weilong.activity.StoreActivity;
import top.drinyeit.weilong.adapter.CommodityDetailBottomAdapter;
import top.drinyeit.weilong.adapter.CommodityDetailCVAdapter;
import top.drinyeit.weilong.model.CategoryGoodsDetailsBean;
import top.drinyeit.weilong.model.PictureBean;
import top.drinyeit.weilong.retrofit.HttpResponse;
import top.drinyeit.weilong.retrofit.RetrofitMethod;
import top.drinyeit.weilong.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.weilong.retrofit.subscriber.SubscriberListener;

import static top.drinyeit.weilong.app.Constant.COMMON_SUCCESS;

/**
 * Created by wu on 2018/1/10.
 */

public class CommodityDetailPresenter {
    private CommodityDetailActivity activity;
    private CommodityDetailCVAdapter commodityDetailCVAdapter;
    private CommodityDetailBottomAdapter commodityDetailBottomAdapter;
    private List<PictureBean> listTop;
    private List<PictureBean> listBottom;

    public CommodityDetailPresenter(CommodityDetailActivity activity) {
        this.activity = activity;
    }

    //获取商品详情
    public void getCommodityDetail(Map<String, Object> map) {
        activity.showLoading();
        CommonSubscriber<HttpResponse<CategoryGoodsDetailsBean>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<CategoryGoodsDetailsBean>>() {
            @Override
            public void onError(String e, int code) {
                activity.dismissLoading();
                activity.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(final HttpResponse<CategoryGoodsDetailsBean> o) {
                activity.dismissLoading();
                listTop = new ArrayList<>();
                listBottom = new ArrayList<>();
                if (o.getCode() == COMMON_SUCCESS) {
                    if (o.getData().getPicturelist() != null && o.getData().getPicturelist() .size() > 0) {
                        for (int i = 0; i < o.getData().getPicturelist().size(); i++) {
                            if (o.getData().getPicturelist().get(i).getPicture_type() == 2) {
                                listTop.add(o.getData().getPicturelist().get(i));
                            }
                            if (o.getData().getPicturelist().get(i).getPicture_type() == 3) {
                                listBottom.add(o.getData().getPicturelist().get(i));
                            }
                        }
                        if (listTop.size() > 0){
                            commodityDetailCVAdapter = new CommodityDetailCVAdapter(activity, listTop);
                            activity.mCarouselView.setAdapter(commodityDetailCVAdapter);
                        }
                        if (listBottom.size() > 0){
                            //底部图片
                            commodityDetailBottomAdapter = new CommodityDetailBottomAdapter(listBottom);
                            activity.mRecyclerView.setLayoutManager(new LinearLayoutManager(activity));
                            activity.mRecyclerView.setAdapter(commodityDetailBottomAdapter);
                            activity.mRecyclerView.setNestedScrollingEnabled(false);
                        }

                    }
                    activity.price.setText("¥" + o.getData().getSale_price());
                    activity.commodity_name.setText(o.getData().getCommodity_name() + "  " + o.getData().getWeight() + "g");
                    activity.store_name.setText(o.getData().getStore_name());
                    activity.stockNumber = o.getData().getStock_number();
                    activity.saldPrice = Double.valueOf(o.getData().getSale_price());
                    activity.store_name.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(activity, StoreActivity.class);
                            intent.putExtra("store_id", o.getData().getStore_id());
                            intent.putExtra("store_name", o.getData().getStore_name());
                            activity.startActivity(intent);
                        }
                    });


                } else {
                    activity.showToastMsg(o.getMessage());
                }
            }
        });
        RetrofitMethod.getInstance().getCommodityDetail(subscriber, map);
    }
}
